package com.inossem.warehousestorage_inossem.adapter.emergencyWarehousing;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.CameraUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.utils.log.LogUtils;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.emergency_warehousing.EmergenWarehousingReceiveOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.emergency_warehousing.EmergencyWarehousingReceiveMaterielDetailActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.extra.InStorageExtra;
import com.inossem.warehousestorage_inossem.bean.emergencyWarehousing.EmergencyWarehousingReceiveOverviewBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.manage.ImageManage;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：wsy on 2017/11/28 14:12
 * <p>
 * 邮箱：445979770@qq.com
 */

public class ReceiveEmergencyWarehousingItemListAdapter extends BaseAdapter {


    private Context mContext;
    private List<EmergencyWarehousingReceiveOverviewBean.DataBean.ItemListBean> mEmergencyWarehousingListBeanItemList;
    private List<EmergencyWarehousingReceiveOverviewBean.DataBean.FileListBean> fileBeanItemList;
    private LayoutInflater inflater;
    private EmergenWarehousingReceiveOverviewActivity.EmergenWarehousingReceiveOverviewListener mEmergencyWarehousingReceiveListener;
    private String mEmergencyType, mCreatTime,mPickingFactorie, mInventoryLocation, mDepartmentDemand, mDemandPeople,mRemark , mMaterielCode , mEmergencyValue;
    private List<EmergencyWarehousingReceiveOverviewBean.DataBean.FileListBean> mLocalFileBean;
    private int inLocalPosition;

    public ReceiveEmergencyWarehousingItemListAdapter(Context context, EmergencyWarehousingReceiveOverviewBean.DataBean data, List<EmergencyWarehousingReceiveOverviewBean.DataBean.ItemListBean> emergencyWarehousingListBeanItemList, List<EmergencyWarehousingReceiveOverviewBean.DataBean.FileListBean> localFileBean ,String materielCode, EmergenWarehousingReceiveOverviewActivity.EmergenWarehousingReceiveOverviewListener emergencyWarehousingReceiveListener) {
        mContext = context;
        mEmergencyType = data.getReceipt_type();//紧急类型
        mEmergencyValue = data.getReceipt_type_name();
        mCreatTime = data.getCreate_time();//创建日期
        mPickingFactorie = data.getFty_name();//领料工厂
        mInventoryLocation = data.getLocation_code()+ " - " + data.getLocation_name();//库存地点
        mDepartmentDemand = data.getDemand_dept();//需求部门
        mDemandPeople = data.getDemand_person();//需求人
        mRemark = data.getRemark();//备注
        mLocalFileBean = localFileBean;
        fileBeanItemList = new ArrayList<>();
        for (int i = 0; i < data.getFile_list().size(); i++) {
            if (data.getFile_list().get(i).getExt().equals("jpg")||data.getFile_list().get(i).getExt().equals("png")||data.getFile_list().get(i).getExt().equals("bmp")) {
                fileBeanItemList.add(data.getFile_list().get(i));
            }
        }
        mEmergencyWarehousingListBeanItemList = emergencyWarehousingListBeanItemList;
        mEmergencyWarehousingReceiveListener = emergencyWarehousingReceiveListener;
        inflater = LayoutInflater.from(context);
        mMaterielCode = materielCode;
    }

    @Override
    public int getCount() {
        return mEmergencyWarehousingListBeanItemList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mEmergencyWarehousingListBeanItemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_emergency_warehousing_receive_item_list_first_item, null);
//            TextView purchaseOrderNumber = (TextView) convertView.findViewById(R.id.purchaseOrderNumber);
            TextView emergencyType = (TextView) convertView.findViewById(R.id.emergencyType);
            TextView creatTime = (TextView) convertView.findViewById(R.id.creatTime);
            TextView pickingFactorie = (TextView) convertView.findViewById(R.id.pickingFactorie);
            TextView inventoryLocation = (TextView) convertView.findViewById(R.id.inventoryLocation);
            TextView departmentDemand = (TextView) convertView.findViewById(R.id.departmentDemand);
            TextView demandPeople = (TextView) convertView.findViewById(R.id.demandPeople);
            TextView remark = (TextView) convertView.findViewById(R.id.remark);
            TextView showAndHide = (TextView) convertView.findViewById(R.id.showAndHide);
//            RelativeLayout linearLayout = (RelativeLayout) convertView.findViewById(R.id.imageLayout);
            ImageView imageView1 = (ImageView) convertView.findViewById(R.id.image1);
            ImageView imageView2 = (ImageView) convertView.findViewById(R.id.image2);
            ImageView imageView3 = (ImageView) convertView.findViewById(R.id.image3);
            ImageView delete1 = (ImageView) convertView.findViewById(R.id.delete1);
            ImageView delete2 = (ImageView) convertView.findViewById(R.id.delete2);
            ImageView delete3 = (ImageView) convertView.findViewById(R.id.delete3);
            PercentRelativeLayout hideLayout = (PercentRelativeLayout) convertView.findViewById(R.id.hideLayout);
            EditText materielCode = (EditText) convertView.findViewById(R.id.materielCode);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
//            purchaseOrderNumber.setText(mPurchaseOrderNumber);
            emergencyType.setText(mEmergencyValue);
            creatTime.setText(mCreatTime);
            pickingFactorie.setText(mPickingFactorie);
            departmentDemand.setText(mDepartmentDemand);
            demandPeople.setText(mDemandPeople);
            inventoryLocation.setText(mInventoryLocation);
            remark.setText(mRemark);
            materielCode.setText(mMaterielCode);
            showAndHide.setOnClickListener((View v) -> {
                if (hideLayout.getVisibility() == View.GONE) {
                    hideLayout.setVisibility(View.VISIBLE);
                    ((TextView) v).setText(mContext.getResources().getString(R.string.activity_receive_delivery_overview_hide_information));
                } else if (hideLayout.getVisibility() == View.VISIBLE) {
                    hideLayout.setVisibility(View.GONE);
                    ((TextView) v).setText(mContext.getResources().getString(R.string.activity_receive_delivery_overview_show_information));
                }
            });
//            materielCode.addTextChangedListener(new QRCodeEditTextWatcher(materielCode) {
//                @Override
//                public void onScan(String text) {
//                    Utils.closeSoftKeyboard(mContext, materielCode);
//                    mInStorageReceiveListener.onNoDoubleClickListener(text);
//                }
//            });

            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    mEmergencyWarehousingReceiveListener.onNoDoubleClickListener(materielCode.getText().toString());
                }
            });
//            EmergencyWarehousingUtils.createEditText(mContext , linearLayout , fileBeanItemList , mLocalFileBean);
            if (fileBeanItemList.size() == 0) {
                imageView3.setBackgroundResource(R.drawable.add_picture);
                imageView3.setOnClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        mEmergencyWarehousingReceiveListener.onCameraClickListener();
                    }
                });
            }
            else if (fileBeanItemList.size() == 1) {

                inLocal(imageView2 , 0);
                imageView2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (inLocal(imageView2 , 0)) {
                            showDialog(imageView2 ,"" ,mLocalFileBean.get(inLocalPosition).getBitmapPath() , true ,0, fileBeanItemList.get(0).getFile_id()  , mEmergencyType);
                            LogUtils.i("path ===== " + mLocalFileBean.get(inLocalPosition).getBitmapPath());
                        }else {
                            showDialog(imageView2 ,fileBeanItemList.get(0).getFile_url(),"" , false ,0, fileBeanItemList.get(0).getFile_id()  , mEmergencyType);
                        }

                    }
                });
                delete2.setBackgroundResource(R.drawable.delete_file);
                delete2.setOnClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        showDeleteDialog(mContext , fileBeanItemList.get(0).getFile_id() , inLocal(imageView2 , 0) , null);
//                        mEmergencyWarehousingReceiveListener.onDeleteImageClickListener(fileBeanItemList.get(0).getUuid() , inLocal(imageView2 , 0) , mEmergencyType);
                    }
                });
                imageView3.setBackgroundResource(R.drawable.add_picture);
                imageView3.setOnClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        mEmergencyWarehousingReceiveListener.onCameraClickListener();
                    }
                });
            }else if (fileBeanItemList.size() == 2) {
                inLocal(imageView1 , 0);
                imageView1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (inLocal(imageView1 , 0)) {
                            showDialog(imageView1 ,"" ,mLocalFileBean.get(inLocalPosition).getBitmapPath() , true ,0, fileBeanItemList.get(0).getFile_id()  , mEmergencyType);
                        }else {
                            showDialog(imageView1 ,fileBeanItemList.get(0).getFile_url(),"" , false ,0, fileBeanItemList.get(0).getFile_id()  , mEmergencyType);
                        }

                    }
                });
                inLocal(imageView2 , 1);
                imageView2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (inLocal(imageView2 , 1)) {
                            showDialog(imageView2 ,"" ,mLocalFileBean.get(inLocalPosition).getBitmapPath() , true ,1, fileBeanItemList.get(1).getFile_id()  , mEmergencyType);
                        }else {
                            showDialog(imageView2 ,fileBeanItemList.get(1).getFile_url(),"" , false ,1, fileBeanItemList.get(1).getFile_id()  , mEmergencyType);
                        }

                    }
                });
//                ImageManage.requestImage(mContext, imageView1, fileBeanItemList.get(0).getImageUrl());
                delete1.setBackgroundResource(R.drawable.delete_file);
                delete1.setOnClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        showDeleteDialog(mContext , fileBeanItemList.get(0).getFile_id() , inLocal(imageView1 , 0) , null);
//                        mEmergencyWarehousingReceiveListener.onDeleteImageClickListener(fileBeanItemList.get(0).getUuid() , inLocal(imageView1 , 0) , mEmergencyType);
                    }
                });
//                ImageManage.requestImage(mContext, imageView2, fileBeanItemList.get(1).getImageUrl());
                delete2.setBackgroundResource(R.drawable.delete_file);
                delete2.setOnClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        showDeleteDialog(mContext , fileBeanItemList.get(1).getFile_id() , inLocal(imageView2 , 1) , null );
//                        mEmergencyWarehousingReceiveListener.onDeleteImageClickListener(fileBeanItemList.get(1).getUuid() , inLocal(imageView2 , 1) , mEmergencyType);
                    }
                });
                imageView3.setBackgroundResource(R.drawable.add_picture);
                imageView3.setOnClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        mEmergencyWarehousingReceiveListener.onCameraClickListener();
                    }
                });
            }else if (fileBeanItemList.size() == 3) {
                inLocal(imageView1 , 0);
                imageView1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (inLocal(imageView1 , 0)) {
                            showDialog(imageView1 ,"" ,mLocalFileBean.get(inLocalPosition).getBitmapPath() , true ,0, fileBeanItemList.get(0).getFile_id()  , mEmergencyType);
                        }else {
                            showDialog(imageView1 ,fileBeanItemList.get(0).getFile_url(),"" , false ,0, fileBeanItemList.get(0).getFile_id()  , mEmergencyType);
                        }

                    }
                });
                inLocal(imageView2 , 1);
                imageView2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (inLocal(imageView2 , 1)) {
                            showDialog(imageView2 ,"" ,mLocalFileBean.get(inLocalPosition).getBitmapPath() , true ,1, fileBeanItemList.get(1).getFile_id()  , mEmergencyType);
                        }else {
                            showDialog(imageView2 ,fileBeanItemList.get(1).getFile_url(),"" , false ,1, fileBeanItemList.get(1).getFile_id()  , mEmergencyType);
                        }

                    }
                });
                inLocal(imageView3 , 2);
                imageView3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (inLocal(imageView3 , 2)) {
                            showDialog(imageView3 ,"" ,mLocalFileBean.get(inLocalPosition).getBitmapPath() , true ,2, fileBeanItemList.get(2).getFile_id()  , mEmergencyType);
                        }else {
                            showDialog(imageView3 ,fileBeanItemList.get(2).getFile_url(),"" , false ,2, fileBeanItemList.get(2).getFile_id()  , mEmergencyType);
                        }

                    }
                });
//                ImageManage.requestImage(mContext, imageView1, fileBeanItemList.get(0).getImageUrl());
                delete1.setBackgroundResource(R.drawable.delete_file);
                delete1.setOnClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        showDeleteDialog(mContext , fileBeanItemList.get(0).getFile_id() ,  inLocal(imageView1 , 0) , null);
//                        mEmergencyWarehousingReceiveListener.onDeleteImageClickListener(fileBeanItemList.get(0).getUuid() ,  inLocal(imageView1 , 0) , mEmergencyType);
                    }
                });
//                ImageManage.requestImage(mContext, imageView2, fileBeanItemList.get(1).getImageUrl());
                delete2.setBackgroundResource(R.drawable.delete_file);
                delete2.setOnClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        showDeleteDialog(mContext , fileBeanItemList.get(1).getFile_id() , inLocal(imageView2 , 1) , null);
//                        mEmergencyWarehousingReceiveListener.onDeleteImageClickListener(fileBeanItemList.get(1).getUuid() , inLocal(imageView2 , 1) , mEmergencyType);
                    }
                });
//                ImageManage.requestImage(mContext, imageView3, fileBeanItemList.get(2).getImageUrl());
                delete3.setBackgroundResource(R.drawable.delete_file);
                delete3.setOnClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        showDeleteDialog(mContext , fileBeanItemList.get(2).getFile_id() , inLocal(imageView3 , 2)  , null);
//                        mEmergencyWarehousingReceiveListener.onDeleteImageClickListener(fileBeanItemList.get(2).getUuid() , inLocal(imageView3 , 2) , mEmergencyType);
                    }
                });
            }
        } else {
            EmergencyWarehousingReceiveOverviewBean.DataBean.ItemListBean materielListBean = mEmergencyWarehousingListBeanItemList.get(position - 1);
            ReceiveEmergencyWarehousingItemListAdapter.ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_emergency_warehousing_receive_item_list, null);
                viewHolder = new ReceiveEmergencyWarehousingItemListAdapter.ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ReceiveEmergencyWarehousingItemListAdapter.ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, materielListBean , position-1);
        }
        return convertView;
    }

    private void initView(ReceiveEmergencyWarehousingItemListAdapter.ViewHolder viewHolder, View view) {
        viewHolder.serialNumber = (TextView) view.findViewById(R.id.serialNumber);
        viewHolder.materielCode = (TextView) view.findViewById(R.id.materielCode);
        viewHolder.materielDescribe = (TextView) view.findViewById(R.id.materielDescribe);
        viewHolder.number = (TextView) view.findViewById(R.id.number);
        viewHolder.storageArea = (TextView) view.findViewById(R.id.storageArea);
        viewHolder.positions = (TextView) view.findViewById(R.id.positions);
    }

    private void initData(Context context, ReceiveEmergencyWarehousingItemListAdapter.ViewHolder viewHolder, View view, EmergencyWarehousingReceiveOverviewBean.DataBean.ItemListBean emergencyWarehousingListBeanItemList , int i) {
        viewHolder.serialNumber.setText(emergencyWarehousingListBeanItemList.getUrgence_rid());
        viewHolder.materielCode.setText(emergencyWarehousingListBeanItemList.getMat_code());
        viewHolder.materielDescribe.setText(emergencyWarehousingListBeanItemList.getMat_name());
        viewHolder.number.setText(emergencyWarehousingListBeanItemList.getQty() + emergencyWarehousingListBeanItemList.getUnit_code());
        viewHolder.storageArea.setText(emergencyWarehousingListBeanItemList.getArea_code());
        viewHolder.positions.setText(emergencyWarehousingListBeanItemList.getPosition_id() + "-" + emergencyWarehousingListBeanItemList.getPosition_code());
//        if (InStorageUtils.checkReceiveItem(materielListBean)) {
//        viewHolder.headLayout.setBackgroundResource(R.color.test_green);
//        } else {
//            viewHolder.headLayout.setBackgroundResource(R.color.view_receive_item_list_dark_row);
//        }
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, EmergencyWarehousingReceiveMaterielDetailActivity.class);
                String serial = emergencyWarehousingListBeanItemList.getUrgence_rid();
                intent.putExtra(InStorageExtra.SERIAL_NUMBER, serial);
                context.startActivity(intent);
            }
        });

    }

    private void showDeleteDialog (Context context , String uuid , boolean local ,Dialog dialogDelete) {
        DialogUtils.getConfirmDialog(context, mContext.getResources().getString(R.string.activity_emergency_warehousing_receive_overview_confirm_deleting_images), new DialogUtils.ConfirmDialogListener() {
            @Override
            public void onConfirm(Dialog dialog) {
                mEmergencyWarehousingReceiveListener.onDeleteImageClickListener(uuid , local , mEmergencyType);
                dialog.cancel();
                if (dialogDelete != null) {
                    dialogDelete.cancel();
                }
//                EmergenWarehousingReceiveOverviewActivity.super.onBackPressed();
            }
        }).show();
    }
    private boolean inLocal (ImageView imageView , int where) {
        if (mLocalFileBean.size() != 0) {
            int a = 1000;
            for (int i = 0; i < mLocalFileBean.size(); i++) {
                if (fileBeanItemList.get(where).getFile_id().equals(mLocalFileBean.get(i).getFile_id())) {
                    a = i;
                }
            }
            if (a == 1000) {
                ImageManage.requestImage(mContext, imageView, fileBeanItemList.get(where).getFile_url());
            }else {
                inLocalPosition = a;
                imageView.setImageBitmap(CameraUtils.uriToBitmap(mLocalFileBean.get(a).getBitmapPath()));
                return true;
            }
        }
        ImageManage.requestImage(mContext, imageView, fileBeanItemList.get(where).getFile_url());

        return false;

    }

    private void showDialog (ImageView imageView,String url , String path , boolean inlocal ,int where , String uuid , String mEmergencyType) {
        DialogUtils.getEmergencyWarehousingBigPictureDialog(mContext,url , path ,inlocal, new DialogUtils.DeleteListener() {

            @Override
            public void onDelect(Dialog dialog) {
                showDeleteDialog(mContext , uuid , inLocal(imageView , where) , dialog);
//                mEmergencyWarehousingReceiveListener.onDeleteImageClickListener(uuid , inLocal(imageView , where) , mEmergencyType);
            }
        }).show();

    }
    private class ViewHolder {
        private TextView serialNumber, materielCode, materielDescribe, number, storageArea, positions ;

//        private PercentRelativeLayout batchLayout, headLayout, contentLayout;
    }

}
